package com.shigu.user.action;

import com.shigu.PageResponse;
import com.shigu.advert.api.AdvertService;
import com.shigu.advert.vo.IndexAdvert;
import com.shigu.appVersion.service.VersionService;
import com.shigu.component.enums.SessionEnum;
import com.shigu.component.shiro.AuthorityUser;
import com.shigu.exceptions.WapException;
import com.shigu.item.bo.GoodsVipInfoRequest;
import com.shigu.item.bo.ItemIdRequest;
import com.shigu.item.service.ItemWapService;
import com.shigu.item.vo.GoodsVipInfoVo;
import com.shigu.item.vo.ItemInfoResponse;
import com.shigu.item.vo.ItemSkuResponse;
import com.shigu.user.bo.IndexAdvertRequest;
import com.shigu.user.service.MallTokenService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

@Controller
@RequestMapping("wapsjson")
public class ItemWapController {
    @Autowired
    ItemWapService itemWapService;
    @Autowired
    MallTokenService mallTokenService;
    @Resource(name = "advertServiceImpl")
    AdvertService advertService;
    @Autowired
    VersionService versionService;

    @RequestMapping("indexSwipe")
    @ResponseBody
    public PageResponse<IndexAdvert> indexAdvert(IndexAdvertRequest request) {
        return advertService.indexSwipe(request.getMainCateId());
    }

    @RequestMapping("itemInfo")
    @ResponseBody
    public ItemInfoResponse itemInfo(@Valid ItemIdRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return itemWapService.itemInfo(request.getItemId());
    }

    @RequestMapping("itemSku")
    @ResponseBody
    public ItemSkuResponse itemSku(@Valid ItemIdRequest request, BindingResult result) throws WapException {
        if (result.hasErrors()) {
            throw new WapException(result.getFieldError().getDefaultMessage());
        }
        return itemWapService.itemSku(request.getItemId());
    }

    @RequestMapping("getGoodsVipInfo")
    @ResponseBody
    public GoodsVipInfoVo getGoodsVipInfo(GoodsVipInfoRequest request, HttpSession session) {
        //公众号要获取，所以去查一下session 有没有用户
        AuthorityUser user = (AuthorityUser) session.getAttribute(SessionEnum.LOGIN_USER.toString());
        Long mallUserId = null;
        if (StringUtils.isBlank(request.getToken())) {
            if (user != null) {
                mallUserId = user.getUserId();
            }
        } else {
            mallUserId = mallTokenService.selUserIdByToken(request.getToken());
        }
        String uri = versionService.versionUri(request.getVersion(), request.getOs());
        return itemWapService.vipItemInfo(Long.valueOf(request.getItemId()), mallUserId,uri);
    }
}
